package fr.cephb.joperon.core.bio;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * An assembly on the human genome
 * This is diferent from an organism, e.g. Homo Sapiens : build36, celera, Jim Waston's genome etc
 * I guess this class will have to be replaced when a bunch of genomes will be sequenced and assembled
 * using the novel methods for sequencing
 * @author lindenb
 *
 */
@XmlRootElement(name="assembly",namespace="http://regulon.cng.fr/")
public class Assembly {
private static final String OPERON_NS="http://regulon.cng.fr/";
private Organism organism;
private String source;
private String version;
private String longDesc;
private int id;

/* HUMAN */

/** ncbi genome */
public static final Assembly HSA_NCBI_BUILD_36=new Assembly(1,"ref",Organism.Homo_sapiens,"36","NCBI Homo-Sapiens Reference build 36");
/** celera genome */
public static final Assembly HSA_CELERA_BUILD_36=new Assembly(2,"cel",Organism.Homo_sapiens,"36","NCBI Homo Sapiens Celera build 36");
/** graig Venter's genome */
public static final Assembly HSA_HUMANREF_36=new Assembly(3,"hur",Organism.Homo_sapiens,"36","NCBI Homo Sapiens Human Reference build 36");


/** Mouse genome */
public static final Assembly MMU_NCBI_BUILD_37=new Assembly(4,"ref",Organism.Mus_musculus,"37","Mus Musculus NCBI build 37");
/** Mouse genome */
public static final Assembly MMU_CELERA_BUILD_37=new Assembly(5,"cel",Organism.Mus_musculus,"37","Mus Musculus Celera build 37");

/** All the assemblies handled by Operon */
private static final Assembly ALL[]=new Assembly[]{
		//human
		HSA_NCBI_BUILD_36,
		HSA_CELERA_BUILD_36,
		HSA_HUMANREF_36,
		//mouse
		MMU_NCBI_BUILD_37,
		MMU_CELERA_BUILD_37
	};




private Assembly(int id,String source, Organism organism, String version,String longDesc)
	{
	this.id=id;
	this.source = source;
	this.organism = organism;
	this.version = version;
	this.longDesc= longDesc;
	}


@XmlElement(name="id",namespace=Assembly.OPERON_NS)
/** unique ID for this assembly */
public int getId() {
	return id;
	}

/** return the index in getAssemblies() */ 
public int getIndex()
	{
	return getId()-1;
	}

@XmlElement(name="taxId",namespace=Assembly.OPERON_NS)
/** NCBI tax ID */
public int getTaxId() {
	return getOrganism().getTaxId();
}

@XmlElement(name="source",namespace=Assembly.OPERON_NS)
/**
 * A 3 letter code for this. e.g. ref pour Homo Sapiens build 36
 * @return a 3 letter code defining the source of this assembly
 *  */ 
public String getSource() {
	return source;
}


@XmlElement(name="version",namespace=Assembly.OPERON_NS)
/** version of this assembly */
public String getVersion() {
	return version;
}

/** the organism of this assembly */
public Organism getOrganism() {
	return organism;
	}

/** name of this assembly */
public String getName() {
	return longDesc;
}

/** concatenate source()+"_"+organism.treeLetterCode+"_"+version */
public String getShortCode() {
	return getSource()+"_"+getOrganism().getTreeLetterCode()+"_"+getVersion();
}
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + id;
	return result;
}



@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Assembly other = (Assembly) obj;
	if (id != other.id)
		return false;
	return true;
	}

/** return all Assembly */
public static Assembly[] getAssemblies()
	{
	return ALL;
	}

/** @returns the default assembly */
public static Assembly getDefaultAssembly()
	{
	return HSA_NCBI_BUILD_36;
	}

/** @return an assembly by its id or null if not found */
public static Assembly getAssemblyById(int id)
	{
	for(Assembly as: ALL)
		{
		if(as.getId()==id) return as;
		}
	return null;
	}

@Override
	public String toString() {
		return getName();
		}
}
